const request = require('../../../utils/request')
Component({
  properties: {
    // 当前页的未分组的数据源
    list: {
      type: Array,
      value: [],
      // observer: function(newVal, oldVal) {
        
      // }
    },
    isShow:{
      type: Boolean,
      value: false
    }
  },

  data: {
    myFormat: ['天','时', '分', '秒'],
    detailInfo: {}, //商品信息
    products:[],
		isPintuan: false, //是否为拼团
		pintuan_list: {}, //拼团信息
		pinTuanList: [],
		guigelist: [],
		morenGuige: {}, //默认规格
		currentTab: 0, // 选择规格
		num: 1, //选择数量
  },

  methods: {
     /**
     *商品详情 
     */
    toDetail(e) {
      console.log(e)
      let id = e.currentTarget.dataset.id
        wx.navigateTo({
            url: '/pages/home/pages/my/product-details/product-details?id=' + id,
        })
    },
    add(e){
      let item = e.currentTarget.dataset.item
      let index = e.currentTarget.dataset.index
      let ntype = e.currentTarget.dataset.ntype
      this.triggerEvent('addCart',{
        item,
        index,
        ntype
      })
    },
    	/**
	 * 加入购物车弹窗
	 */
	showCartWindow(e) {
    this.setData({
      currentTab: 0
    })
    this.getDetailInfo(e)
 
  },
  	/**
	 * 规格
	 */
	switchNav(e) {
		const index = e.currentTarget.dataset.index;
		let item = e.currentTarget.dataset.item
		this.setData({
			currentTab: index, // 更新选中的tab
			morenGuige: item,
			num: 1
		});
	},


	/**
	 * 输入框事件
	 */
	bindManual: function (e) {
    var num = e.detail.value;
    if(this.data.detailInfo.is_pin && num >= this.data.detailInfo.max_nums){
      return getApp().toast('最多限购'+this.data.detailInfo.max_nums+'件')
    }
		this.setData({
			num: num
		});
  },
  
    /**
   * 输入框事件
   */
  bindCardManual: function (e) {
    var num = e.detail.value;
    this.setData({
      num: num
    });
  },
  addCardNum(e) {
    let type = e.currentTarget.dataset.type
    let num = this.data.num
    let morenGuige = this.data.morenGuige
    if (type == 1) {
      // 减
      if (num == 1) {
        return getApp().toast('不能再减少了~')
      }
      num--
    } else {
      if (num >= morenGuige.kucun) {
        return getApp().toast('商品库存不足~')
      }
      if(this.data.detailInfo.is_pin && num >= this.data.detailInfo.max_nums){
        return getApp().toast('最多限购'+this.data.detailInfo.max_nums+'件')
      }
      if(this.data.detailInfo.is_seckill && num >= this.data.detailInfo.seckill_rule.max_nums){
        return getApp().toast('最多限购'+this.data.detailInfo.seckill_rule.max_nums+'件')
      }
      num++
    }
    this.setData({
      num
    })
  },
  	// 获取详情页信息
	getDetailInfo(e) {
    let type = e.currentTarget.dataset.type
		let addcar = e.currentTarget.dataset.addcar
    let index = e.currentTarget.dataset.index
    let item = e.currentTarget.dataset.item
    let ntype = e.currentTarget.dataset.ntype
		let _this = this
		request._post('/api?method=goods.goodsinfo', {
			goods_id: item.goods_id
		}).then(res => {
      let list = res.data.products
      list.forEach((obj)=>{
        obj.addSwitch = false
        if (obj.cart_num) {
          obj.addSwitch = true
        }
      })
			_this.setData({
        detailInfo: res.data,
        products: list,
				isPintuan: res.data.is_pin,
				pintuanInfo: res.data.tuan,
				pintuan_list:  res.data.tuan ? res.data.tuan.pin_users :[],
        morenGuige: list ? list[0] :{},
        num: list ? list[0].cart_num > 0 ? list[0].cart_num : 1 : 1,
				guigelist: list,
        isShowCart: true,
        isShowCart_type: type,
        isAddCar: addcar == 1 ? true : false,
        item: item,
        index,
        ntype
			})
  
		})
  },
	hideCartWindow() {
		this.setData({
			isShowCart: false
		})
  },
  confirmOrder(){
    this.hideCartWindow()
    let morenGuige = this.data.morenGuige
    morenGuige.cart_num = this.data.num - 1
    this.setData({
      morenGuige: morenGuige
    })
    this.triggerEvent('addCart',{
      item: this.data.morenGuige,
      index: this.data.index,
      ntype: this.data.ntype
    })
  },
    addCartReduce(e){
      let type = e.currentTarget.dataset.type
      let item = e.currentTarget.dataset.item
      let index = e.currentTarget.dataset.index
      let ntype = e.currentTarget.dataset.ntype
      if(type == 2 && item.seckill_rule && item.cart_num == item.seckill_rule.max_nums){
        wx.showToast({
          title:`最多限购${item.seckill_rule.max_nums}件`,
          icon:'none',
          duration:1000
        })
        return
      }
      this.triggerEvent('addCartReduce',{
        type,
        item,
        index,
        ntype
      })
    }
  },
})